﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;

namespace Feniks
{
    public partial class frmKoliDara : DevExpress.XtraEditors.XtraForm
    {
        public frmKoliDara()
        {
            InitializeComponent();
        }

        private void frmKoliDara_Load(object sender, EventArgs e)
        {
            using (DataEntities ent = new DataEntities())
            {
                foreach (var item in ent.KoliDaralari.ToList())
                {
                    cmbKoli.Properties.Items.Add(new ComboItem()
                    {
                        Text = item.Adi,
                        Value = item
                    });
                }
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            using (DataEntities ent = new DataEntities())
            {
                var dara = ent.KoliDaralari.Where(x => x.Adi == cmbKoli.Text).FirstOrDefault();
                if (dara != null)
                {
                    dara.Agirlik = Cevir.Parse<double>(txtDara.Text);
                }
                else
                {
                    KoliDara dr = new KoliDara();
                    dr.Adi = cmbKoli.Text;
                    dr.Agirlik = Cevir.Parse<double>(txtDara.Text);
                    ent.AddToKoliDaralari(dr);
                }
                ent.SaveChanges();
            }
            txtDara.Text = "";
            cmbKoli.Text = "";
            MessageBox.Show("Kaydedildi !");
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            using (DataEntities ent = new DataEntities())
            {
                var dara = ent.KoliDaralari.Where(x => x.Adi == cmbKoli.Text).FirstOrDefault();
                if (dara != null)
                {
                    ent.DeleteObject(dara);
                }
            }
            txtDara.Text = "";
            cmbKoli.Text = "";
            MessageBox.Show("Silindi !");
        }

        private void cmbKoli_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbKoli.SelectedIndex > -1)
            {
                using (DataEntities ent = new DataEntities())
                {
                    var dara = ent.KoliDaralari.Where(x => x.Adi == cmbKoli.Text).FirstOrDefault();
                    if (dara != null)
                    {
                        txtDara.Text = dara.Agirlik.ToString();
                    }
                }
            }
        }
    }
}